﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
' http://dnntweet.codeplex.com
'
' Copyright (c) 2011, Will Strohl
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this 
' software and associated documentation files (the "Software"), to deal in the Software 
' without restriction, including without limitation the rights to use, copy, modify, 
' merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
' permit persons to whom the Software is furnished to do so, subject to the following 
' conditions:
'
' The above copyright notice and this permission notice shall be included in all copies 
' or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
' PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
' HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
' OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'

Imports System.Text
Imports System.Web
Imports System.Web.SessionState

Namespace WillStrohl.Modules.Twitter

    Public Class SearchHistoryHandler
        Implements System.Web.IHttpHandler, IRequiresSessionState

#Region " Private Members "

        Private p_SearchTerms As String = Null.NullString
        Private p_Action As String = Null.NullString
        Private p_Moduleid As Integer = Null.NullInteger
        Private p_UserId As Integer = Null.NullInteger

#End Region

#Region " Properties "

        Private ReadOnly Property SearchTerms() As String
            Get
                Return Me.p_SearchTerms
            End Get
        End Property

        Private ReadOnly Property Action() As String
            Get
                Return Me.p_Action
            End Get
        End Property

        Private ReadOnly Property ModuleId() As Integer
            Get
                Return Me.p_Moduleid
            End Get
        End Property

        Private ReadOnly Property UserId() As Integer
            Get
                Return Me.p_UserId
            End Get
        End Property

#End Region

        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

            context.Response.ContentType = "text/plain"

            If Not context.Request.QueryString("st") Is Nothing Then
                Me.p_SearchTerms = context.Request.QueryString("st").ToString
            Else
                Throw New ArgumentNullException("st")
            End If

            If Not context.Request.QueryString("ac") Is Nothing Then
                Me.p_Action = context.Request.QueryString("ac").ToString
            Else
                Throw New ArgumentNullException("ac")
            End If

            If Not context.Request.QueryString("mid") Is Nothing Then
                Me.p_Moduleid = Integer.Parse(context.Request.QueryString("mid").ToString, Globalization.NumberStyles.Integer)
            Else
                Throw New ArgumentNullException("mid")
            End If

            If Not context.Request.QueryString("uid") Is Nothing Then
                Me.p_UserId = Integer.Parse(context.Request.QueryString("uid").ToString, Globalization.NumberStyles.Integer)
            Else
                Throw New ArgumentNullException("uid")
            End If


            Dim ctlModule As New TwitterController

            If String.Equals(Me.Action, "save", StringComparison.InvariantCultureIgnoreCase) Then

                If Not ctlModule.DoesSearchHistoryExist(Me.UserId, Me.SearchTerms) Then
                    ctlModule.AddSearchHistory(Me.ModuleId, Me.UserId, Me.SearchTerms)
                End If
                context.Response.Write("true")

            ElseIf String.Equals(Me.Action, "get", StringComparison.InvariantCultureIgnoreCase) Then

                context.Response.Write(GetSearchHistory)

            End If

            context.Response.End()

        End Sub

        Private Function GetSearchHistory() As String

            Dim ctlModule As New TwitterController
            Dim collSearch As New SearchHistoryInfoCollection
            collSearch = ctlModule.GetSearchHistoryByModuleIdLike(Me.ModuleId, Me.SearchTerms)

            If Not collSearch Is Nothing AndAlso collSearch.Count > 0 Then

                Dim intCount As Integer = 0
                Dim sb As New StringBuilder(20)
                sb.Append("[")

                For Each oSearch As SearchHistoryInfo In collSearch

                    sb.AppendFormat("""{0}""", oSearch.SearchText)

                    If Not intCount = collSearch.Count - 1 Then
                        sb.Append(", ")
                    End If

                    intCount += 1
                Next

                sb.Append("]")

                Return sb.ToString

            Else
                Return Null.NullString
            End If

        End Function

#Region " IHttpHandler Implementation "

        ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property

#End Region

    End Class

End Namespace